package dragon.modules.system.user.dao;

import java.util.List;
import java.util.Map;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.po.SysUsermanagementroles;

public class UserManagementRolesDao {

	public boolean save(SysUsermanagementroles po) throws ExceptionHandler {
		return DML.saveOrUpdateObject(po) > 0 ? true : false;
	}

	public boolean delete(SysUsermanagementroles po) throws ExceptionHandler {
		if (po.getUsergroupid() == 0) {
			return false;
		}
		if (po.getUserid() == 0) {
			return false;
		}
		return DML.prepareExecuteUpdate("delete from " + SysUsermanagementroles.tableName + " where userid=? and usergroupid=?" ,po.getUserid(),po.getUsergroupid()) > 0 ? true : false;
	}

	public void delete(int userid) throws ExceptionHandler {
		DML.prepareExecuteUpdate("delete from " + SysUsermanagementroles.tableName + " where userid=?",userid);
	}

	public int[] getGroupIdByUserID(int id) throws ExceptionHandler {
		List<Map<String, Object>> list = DML.query("select * from " + SysUsermanagementroles.tableName + " where userid=" + id);
		if (list.size() > 0) {
			int[] ids = new int[list.size()];
			for (int i = 0; i < list.size(); i++) {
				ids[i] = Integer.parseInt(list.get(i).get("USERGROUPID").toString());
			}
			return ids;
		} else {
			return null;
		}
	}

}
